<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator-stu_rewards="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>通知通知</el-breadcrumb-item>
            <el-breadcrumb-item>通知列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="未读通知" name="first">
                <!-- 搜索 添加 -->
                <el-row :gutter="20">
                    <el-col :span="10">
                        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUnReadList">
                            <el-button type="success" slot="append" icon="el-icon-search" @click="getUnReadList"></el-button>
                        </el-input>
                    </el-col>
                </el-row>
                <el-alert
                        class="msg_title"
                        :title="msg_unread_title"
                        type="error"
                        effect="dark"
                >
                </el-alert>
                <el-card shadow="always" v-for="msg in unReadList" :key="msg.id" class="msg_card">
                    <div class="header">
                        <span>{{msg.type}} - {{msg.type_2}}</span>
                        <span v-if="msg.is_read == 0" style="float: right; font-size:15px;color: black">未读 | <span @click="mark_read(msg)" style="margin-left:5px; cursor: pointer;color: dodgerblue">标为已读</span></span>
                        <span v-else style="cursor: pointer; float: right; font-size:15px; color: black">已读</span>
                    </div>
                    <div class="text item body">
                        <p style="text-indent: 20px; line-height: 30px">{{msg.message}}</p>
                        <p style="float: right; margin-right: 50px;font-size: 15px;font-weight: bolder">发给： {{msg.user.nickname}} &nbsp;&nbsp; 时间： {{msg.created_at | datetimeFormat}}</p>
                    </div>
                </el-card>
                <!-- 分页区域 -->
                <div stu_rewards="pager-box">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryInfo.pageNum"
                            :page-sizes="[2, 5, 10, 15]"
                            :page-size="queryInfo.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            prev-text="上一页"
                            next-text="下一页"
                            :total="total"
                            background
                            hide-on-single-page
                    ></el-pagination>
                </div>
            </el-tab-pane>
            <el-tab-pane label="已读通知" name="second">
                <!-- 搜索 添加 -->
                <el-row :gutter="20">
                    <el-col :span="10">
                        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getReadList">
                            <el-button type="success" slot="append" icon="el-icon-search" @click="getReadList"></el-button>
                        </el-input>
                    </el-col>
                </el-row>
                <el-alert
                        class="msg_title"
                        :title="msg_read_title"
                        type="success"
                        effect="dark"
                ></el-alert>
                <el-card shadow="always" v-for="msg in ReadList" :key="msg.id" class="msg_card">
                    <div class="header">
                        <span>{{msg.type}} - {{msg.type_2}}</span>
                        <span style="cursor: pointer; float: right; font-size:15px; color: black">已读</span>
                    </div>
                    <div class="text item body">
                        <p style="text-indent: 20px; line-height: 30px">{{msg.notice}}</p>
                        <p style="float: right; margin-right: 50px;font-size: 15px;font-weight: bolder">发给： {{msg.user.nickname}} 时间： {{msg.created_at | datetimeFormat}}</p>
                    </div>
                </el-card>
                <!-- 分页区域 -->
                <div stu_rewards="pager-box">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryInfo.pageNum"
                            :page-sizes="[2, 5, 10, 15]"
                            :page-size="queryInfo.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            prev-text="上一页"
                            next-text="下一页"
                            :total="total"
                            background
                            hide-on-single-page
                    ></el-pagination>
                </div>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
    export default {
        data () {
            return {
                queryInfo: {
                    query: '',
                    // 当前页数
                    pageNum: 1,
                    // 每页显示多少数据
                    pageSize: 10,
                    type: '',
                    is_read: 0,
                },
                total: 0,
                unread_total: 0,
                read_total: 0,
                // 所有学生获奖列表
                activeName: 'first',
                unReadList: [],
                ReadList: [],
                tabname: 'first',
                msg_unread_title: '',
                msg_read_title: '',
            }
        },
        created () {
            this.getData();
        },
        methods: {
            handleSizeChange (newSize) {
                this.queryInfo.pageSize = newSize;
                if (this.tabname === 'first'){
                    this.getUnReadList();
                }else if (this.tabname === "second"){
                    this.getReadList();
                }
            },
            handleCurrentChange (newSize) {
                this.queryInfo.pageNum = newSize;
                if (this.tabname === 'first'){
                    this.getUnReadList();
                }else if (this.tabname === "second"){
                    this.getReadList();
                }
            },
            handleClick(tab, event) {
                this.tabname = tab.paneName;
                if (tab.paneName === 'first'){
                    this.getUnReadList();
                }else if (tab.paneName === "second"){
                    this.getReadList();
                }
            },
            async getData(){
                this.getUnReadList();
            },
            async getUnReadList() {
                this.queryInfo.type = "通知";
                this.queryInfo.is_read = 0;
                const {data: res, status: status} = await this.axios.get('message/getMessageList', {params: this.queryInfo});
                if (status !== 200) {
                    return this.$notice.error('获取学生列表失败！')
                }
                this.unReadList = res.data.notices;
                this.total = res.data.total;
                this.msg_unread_title = "共有未读通知 " + res.data.total + " 条";
                this.unread_total = res.data.total;
            },
            async getReadList() {
                this.queryInfo.type = "通知";
                this.queryInfo.is_read = 1;
                const {data: res, status: status} = await this.axios.get('message/getMessageList', {params: this.queryInfo});
                if (status !== 200) {
                    return this.$notice.error('获取通知列表失败！')
                }
                this.ReadList = res.data.notices;
                this.total = res.data.total;
                this.msg_read_title = "共有已读通知 " + res.data.total + " 条";
                this.read_total = res.data.total;
            },
            async mark_read (msg) {
                const { data: res, status: status } = await this.axios.put('notice/updateMessageIsRead', {"id": msg.id});
                if (status !== 200) return this.$notice.error('标为已读失败！');
                msg.is_read = 1;
            },
        }
    }
</script>
<style lang="less" scoped>
.msg_card{
    min-height: 120px;
    padding-bottom: 30px;
    border-bottom: 1px solid #1e90ff;
    border-radius: 10%;
}
.msg_card .header{
    color: red;
    padding: 10px;
    background-color: pink;

}
.msg_card .body {
    margin-top: 10px;
}
.msg_title{
    width: 400px;
    margin: 0 20px;
}
</style>